package com.gxc.controller;

import com.gxc.entity.User;
import com.gxc.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.coyote.OutputBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("user")
@Slf4j
public class UserController {

    // 创建日志对象
//    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService userService;
    @RequestMapping("findAll")
    public String findAll(Model model){
        List<User> all = userService.findAll();
        model.addAttribute("users", all);
        log.info("logger--info---");
        log.debug("logger--debug---");
        return   "showAll";
    }

    @ResponseBody
    @GetMapping("delete")
    public String delete(String id){
        System.out.println(id+"id-----------");
        return id;
    }

    @GetMapping("save")
    public String save(User user){
        userService.save(user);
        // 对于没有返回值的方法需要让其跳转到
        // 某个特定页面，否则会造成地址栏地址不刷新，表单重复提交
        return "redirect:/user/findAll";
    }
}
